CREATE PROCEDURE coreCmsAdxGetByParentID
(
	@ParentID			int,
	@SortExpression		nvarchar(50),
	@SortDirection		int
)	
AS
BEGIN
	IF @SortDirection = 1 
	BEGIN
	
		IF @SortExpression = 'RANK'
			SELECT	c.PK_ID, c.FK_STypeID, c.FK_ParentID, c.Rank, c.NodeTitle, c.NodeName, c.NodePath, 
					c.CreatedDT, c.FK_CreatedBy, c.LastModifiedDT, c.FK_LastModifiedBy, c.WorkflowModeID,
					c.WorkflowStatus, c.FK_CheckedOutTo, c.CheckedOutDT, c.CurrentVersionNumber,
					c.HasEmbargoStart, c.PublishStartDT, c.HasEmbargoEnd, c.PublishUntilDT,
					c.ExpiryActionID, c.ExpiryActionDataID, c.HasOwnSecurity,
					v.FK_CheckedInBy, v.CheckedInDT, v.VersionNumber, v.VersionNotes, v.XMLData
			FROM Content c INNER JOIN ContentV v ON v.PK_ID=c.FK_VersionV
			WHERE c.FK_ParentID=@ParentID ORDER BY c.Rank;
			
		IF @SortExpression = 'TITLE'
			SELECT	c.PK_ID, c.FK_STypeID, c.FK_ParentID, c.Rank, c.NodeTitle, c.NodeName, c.NodePath, 
					c.CreatedDT, c.FK_CreatedBy, c.LastModifiedDT, c.FK_LastModifiedBy, c.WorkflowModeID,
					c.WorkflowStatus, c.FK_CheckedOutTo, c.CheckedOutDT, c.CurrentVersionNumber,
					c.HasEmbargoStart, c.PublishStartDT, c.HasEmbargoEnd, c.PublishUntilDT,
					c.ExpiryActionID, c.ExpiryActionDataID, c.HasOwnSecurity,
					v.FK_CheckedInBy, v.CheckedInDT, v.VersionNumber, v.VersionNotes, v.XMLData
			FROM Content c INNER JOIN ContentV v ON v.PK_ID=c.FK_VersionV
			WHERE c.FK_ParentID=@ParentID ORDER BY c.NodeTitle;
	
	END
	ELSE
	BEGIN
	
		IF @SortExpression = 'RANK'
			SELECT	c.PK_ID, c.FK_STypeID, c.FK_ParentID, c.Rank, c.NodeTitle, c.NodeName, c.NodePath, 
					c.CreatedDT, c.FK_CreatedBy, c.LastModifiedDT, c.FK_LastModifiedBy, c.WorkflowModeID,
					c.WorkflowStatus, c.FK_CheckedOutTo, c.CheckedOutDT, c.CurrentVersionNumber,
					c.HasEmbargoStart, c.PublishStartDT, c.HasEmbargoEnd, c.PublishUntilDT,
					c.ExpiryActionID, c.ExpiryActionDataID, c.HasOwnSecurity,
					v.FK_CheckedInBy, v.CheckedInDT, v.VersionNumber, v.VersionNotes, v.XMLData
			FROM Content c INNER JOIN ContentV v ON v.PK_ID=c.FK_VersionV
			WHERE c.FK_ParentID=@ParentID ORDER BY c.Rank DESC;
			
		IF @SortExpression = 'TITLE'
			SELECT	c.PK_ID, c.FK_STypeID, c.FK_ParentID, c.Rank, c.NodeTitle, c.NodeName, c.NodePath, 
					c.CreatedDT, c.FK_CreatedBy, c.LastModifiedDT, c.FK_LastModifiedBy, c.WorkflowModeID,
					c.WorkflowStatus, c.FK_CheckedOutTo, c.CheckedOutDT, c.CurrentVersionNumber,
					c.HasEmbargoStart, c.PublishStartDT, c.HasEmbargoEnd, c.PublishUntilDT,
					c.ExpiryActionID, c.ExpiryActionDataID, c.HasOwnSecurity,
					v.FK_CheckedInBy, v.CheckedInDT, v.VersionNumber, v.VersionNotes, v.XMLData
			FROM Content c INNER JOIN ContentV v ON v.PK_ID=c.FK_VersionV
			WHERE c.FK_ParentID=@ParentID ORDER BY c.NodeTitle DESC;
	END
END
